<template>
  <div >
<!--    头部-->
    <div style="display: flex; height: 60px; line-height: 60px; border-bottom: 1px solid #eee; background-color: #edeae2;">
      <div style="width: 300px; display: flex; padding-left: 30px;margin-top: 5px">
        <div style="width: 50px">
          <img src="../../assets/logo.svg" alt="" style="width: 40px; position: relative; top: 8px; right: 0">
        </div>
        <div style="flex: 1">唐朝历史人物分析可视化平台</div>
      </div>
      <div style="flex: 1">

        <el-menu :default-active="$route.path" mode="horizontal" router style="border: none; flex-wrap: nowrap;" background-color="#edeae2">
          <el-menu-item index="/front/home">首页</el-menu-item>
          <el-menu-item index="/front/type">人物信息</el-menu-item>
          <el-menu-item index="/front/poet">诗人信息</el-menu-item>
          <!-- <el-menu-item index="/front/message">我的反馈</el-menu-item>
          <el-menu-item index="/front/collect">我的收藏</el-menu-item> -->
          <el-menu-item index="/front/analysis/populationStatistics">人口统计</el-menu-item>
          <!-- <el-menu-item index="/front/analysis/populationMigration">人口迁徙</el-menu-item> -->
          <el-menu-item index="/front/analysis/populationDieAddress">生卒地变迁</el-menu-item>
          <el-menu-item index="/front/analysis/populationSurname">姓氏统计</el-menu-item>
          <el-menu-item index="/front/analysis/populationRelationship">人物关系</el-menu-item>
          <el-menu-item index="/front/analysis/populationRelationshipNetwork">诗人网络</el-menu-item>
        </el-menu>
      </div>
      <div class="m-r-10">
        <div v-if="!user.username" style="text-align: right; padding-right: 30px">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown style="width: 150px; cursor: pointer; text-align: right">
            <div style="display: inline-block">
              <img :src="user.avatar" alt=""
                   style="width: 30px; border-radius: 50%; position: relative; top: 10px; right: 5px">
              <span>{{ user.name }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
            </div>
            <el-dropdown-menu slot="dropdown" style="width: 100px; text-align: center">
              <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                <span style="text-decoration: none" @click="password"><div>修改密码</div></span>
              </el-dropdown-item>
              <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                <span style="text-decoration: none" @click="person"><div>个人信息</div></span>
              </el-dropdown-item>
              <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                <span style="text-decoration: none" @click="$router.push('/front/message')"><div>我的反馈</div></span>
              </el-dropdown-item>
              <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                <span style="text-decoration: none" @click="$router.push('/front/collect')"><div>我的收藏</div></span>
              </el-dropdown-item>
              <el-dropdown-item style="font-size: 14px; padding: 5px 0">
                <span style="text-decoration: none" @click="logout"><div>退出</div></span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>

    <div>
      <router-view  @refreshUser="getUser" />
    </div>
  </div>
</template>

<script>
export default {
  name: "Front",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {

  },
  methods: {
    person(){
      this.$router.push('/front/person')
    },
    password(){
      this.$router.push('/front/password')
    },
    logout() {
      localStorage.removeItem('user')  // 清除当前的token和用户数据
      this.$router.push('/login')
      this.$message.success("退出成功")
    },
    getUser() {
      let username = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")).username : ""
      if (username) {
        // 从后台获取User数据
        this.request.get("/user/username/" + username).then(res => {
          // 重新赋值后台的最新User数据
          this.user = res.data
        })
      }
    }
  }
}
</script>

<style>
.item{
  display: inline-block;
  width: 100px;

  text-align: center;
  cursor: pointer
}
.item a {
  color: 	#1E90FF;
}
.item:hover{
  background-color: 	LightPink;
}
</style>
